﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Caching;
using System.Collections;
using System.Web;
using System.IO;
using System.Net;
using System.Configuration;

namespace LczWebPart
{
    public class HTMLCache
    {
        Cache cache = System.Web.HttpContext.Current.Cache;
        Hashtable ht = new Hashtable();
        Hashtable htPage = new Hashtable();
        bool ioHtml = new bool();

        public HTMLCache()
        { }
        public void AddHTMLCache()
        {
            IoHtml = true;            
            //-----------------------------------------------------------------
            ht.Add("1", "/_Layouts/LczWebPart/Skin/1.htm"); //1模板
            ht.Add("WebPartSkin1", "/_Layouts/LczWebPart/Skin/WebPartSkin1.htm");//WebPartSkin1
            ht.Add("WebPartSkin2", "/_Layouts/LczWebPart/Skin/WebPartSkin2.htm");//WebPartSkin2
            ht.Add("ListSkin1", "/_Layouts/LczWebPart/Skin/ListSkin1.htm");//ListSkin1
            ht.Add("ListSkin1_Row", "/_Layouts/LczWebPart/Skin/ListSkin1_Row.htm");//ListSkin1_Row

            ht.Add("FileDownLoadItemLi", "/_Layouts/LczWebPart/HTML/ItemLi.htm");
            CacheAll();
        }
        /// <summary>
        /// 带参创建缓存
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public HTMLCache(string key, string value)
        {
            if (cache[key] == null)
            {
                cache[key] = GetHtml(value);
            }
        }
        /// <summary>
        /// 带Hashtable创建缓存
        /// </summary>
        /// <param name="ht">缓存对象</param>
        public HTMLCache(Hashtable ht)
        {
            foreach (DictionaryEntry de in ht)
            {
                cache[de.Key.ToString()] = GetHtml(de.Value.ToString());
            }
        }
        /// <summary>
        /// 读取方式文本或网页（默认文本）
        /// </summary>
        public bool IoHtml
        {
            get { return ioHtml; }
            set { ioHtml = value; }
        }
        /// <summary>
        /// 缓存保存
        /// </summary>
        public Hashtable Ht
        {
            get { return ht; }
            set { ht = value; }
        }
        /// <summary>
        /// 动态数据缓存
        /// </summary>
        public Hashtable HtPage
        {
            get { return htPage; }
            set { htPage = value; }
        }
        /// <summary>
        /// 缓存对象
        /// </summary>
        public void CachePage()
        {
            foreach (DictionaryEntry de in htPage)
            {//if (cache[de.Key.ToString()] == null) 
                cache[de.Key.ToString()] = de.Value;
            }
        }
        /// <summary>
        /// 缓存所有页面
        /// </summary>
        public void CacheAll()
        {
            foreach (DictionaryEntry de in ht)
            {                
                //if (cache[de.Key.ToString()] == null)
                //{
                    cache[de.Key.ToString()] = GetHtml(de.Value.ToString());// "/_Layouts/LczWebPart" + de.Value.ToString();
                //}
                
            }
        }
        /// <summary>
        /// 获取Html模板内容
        /// </summary>
        /// <param name="fileName">文件名称</param>
        /// <returns>内容</returns>
        string GetHtml(string fileName)
        {
            string html = string.Empty;
            string filepath = string.Empty;
            //string filemap =//ConfigurationManager.AppSettings["HTMLFile"].ToString();
            //string webUrl =//ConfigurationManager.AppSettings["WebUrl"].ToString();
            if (ioHtml)
            {
                //filepath = HttpContext.Current.Request.PhysicalApplicationPath + filemap + "\\" + fileName;
                filepath = HttpContext.Current.Server.MapPath("~"+ fileName);// "/_Layouts/LczWebPart" + fileName;
                if (File.Exists(filepath))
                {
                    html = File.ReadAllText(filepath, System.Text.Encoding.Default); 
                } 
            }
            else
            {
                //filepath = webUrl + filemap + "\\" + fileName;
                filepath = "\\" + fileName;
                html = GetHttpData(filepath);
            }
            return html;
        }
        /// <summary>
        /// 下载url数据
        /// </summary>
        /// <param name="url">地址</param>
        /// <returns>返回字符串</returns>
        private static string GetHttpData(string str)
        {
            WebClient wc = new WebClient();
            try
            {
                str = wc.DownloadString(str);
                return str;
            }
            catch
            {
                return null;
            }
        }
        /// <summary>
        /// 移除所有缓存
        /// </summary>
        public void RemoveAll()
        {
            foreach (DictionaryEntry de in Ht)
            {
                cache.Remove(de.Key.ToString());
            }
        }
        /// <summary>
        /// 移除缓存
        /// </summary>
        /// <param name="CacheName">缓存名</param>
        public void RemoveOnly(string CacheName)
        {
            htPage.Remove(CacheName);
            ht.Remove(CacheName);
            cache.Remove(CacheName);
        }
    }

}
